//
//  XSOriginalView.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/7.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

class XSOriginalView: UIView {

    
    var statusViewModel:XSStatusViewModel? {
        didSet{
            
            // 设置显示的头像
            if let urlString = statusViewModel?.status?.user?.profile_image_url, let url = NSURL(string: urlString){
                headImgView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default"))
            }
            // 设置昵称
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            // 设置时间和来源 TODO: 以后会专门处理
            timeLabel.text = statusViewModel?.createText
            sourceLabel.text = statusViewModel?.sourceText
            // 设置认证图标
            verifiedImage.image = statusViewModel?.verifiedImage
            // 设置vip图标
            membershipImage.image = statusViewModel?.membershipImage
            // 设置内容
            contentLabel.text = statusViewModel?.status?.text

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
   
    private func setupUI(){
        
        addSubview(headImgView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(membershipImage)
        addSubview(verifiedImage)
        addSubview(contentLabel)
       
        
        headImgView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(XSStatusCellMargin)
            make.width.height.equalTo(XSStatusCellHeadImageWH)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImgView.snp_trailing).offset(XSStatusCellMargin)
            make.top.equalTo(headImgView.snp_top)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImgView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(XSStatusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImgView.snp_bottom).offset(XSStatusCellMargin)
            make.leading.equalTo(headImgView)
            make.trailing.equalTo(self).offset(-XSStatusCellMargin)
        }
        
        membershipImage.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(XSStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        verifiedImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImgView.snp_trailing).offset(-2)
            make.centerY.equalTo(headImgView.snp_bottom).offset(-2)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel)
        }
    }
    
    //懒加载头像View
    private lazy var headImgView:UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    
    //懒加载nameLabel昵称
    private lazy var nameLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: 14)
        label.text = "我是焊工"
        
        return label
    }()
    
    //懒加载时间label
    private lazy var timeLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
        label.text = "刚刚"
        return label
    }()
    
    //懒加载SourceLable
    private lazy var sourceLabel:UILabel = {
        let label = UILabel(textColor: UIColor.redColor(), fontSize: 12)
        
        label.text = "来自iPhone 7"
        return label
    }()
    
    //懒加载内容label
    private lazy var contentLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 14)
        label.text = "这是一条神奇的微博"
    
        label.numberOfLines = 0
        return label
    }()
    
    //懒加载会员等级图标
    private lazy var membershipImage:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //懒加载V认证
    private lazy var verifiedImage:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
        
}

